#include "form.h"
#include "ui_form.h"
#include "DataBase.h"

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
}

Form::~Form()
{
    delete ui;
}

void Form::on_pushButton_clicked()
{
    DataBase date("customer","123456");
    QVector<QVector<QString>> data = date.libQuery(ui->lineEdit->text(), ui->lineEdit_2->text(), ui->lineEdit_3->text(), ui->lineEdit_4->text());
    int rowCount = data.size();
    int totalNum=date.BooksCount();
    int num=date.CategoryCount();
    QString message = "图书信息查询 SQL 执行成功\n查到 " + QString::number(rowCount) + " 条数据,该图书馆共有"+QString::number(totalNum)+"本书,共有"+QString::number(num)+"类书";
    QMessageBox::information(this, "信息", message);

    ui->tableWidget->setRowCount(7);
    ui->tableWidget_2->setRowCount(qMin(rowCount - 7, 7)); // 设置第二个表格的行数为7或剩余行数的较小值

    for (int i = 0; i < rowCount; i++)
    {
        if (i < 7)
        {
            QVector<QString> rowData = data[i];
            int columnCount = qMin(rowData.size(), ui->tableWidget->columnCount());

            for (int j = 0; j < columnCount; j++)
            {
                ui->tableWidget->setItem(i, j, new QTableWidgetItem(rowData[j]));
            }
        }
        else if (i >= 7)
        {
            int rowIndex = i - 7;

            // 设置左边序列显示的行号为 8 到 14
            QTableWidgetItem *rowHeader = new QTableWidgetItem(QString::number(i + 1)); // 显示的行号为 8 到 14
            ui->tableWidget_2->setVerticalHeaderItem(rowIndex, rowHeader);

            // 填充数据到表格中
            QVector<QString> rowData = data[i];
            int columnCount = qMin(rowData.size(), ui->tableWidget_2->columnCount());

            for (int j = 0; j < columnCount; j++)
            {
                ui->tableWidget_2->setItem(rowIndex, j, new QTableWidgetItem(rowData[j])); // 在列表对应的位置输出
            }
        }
    }
}


void Form::on_pushButton_2_clicked()
{
    int nextPageIndex = (ui->stackedWidget->currentIndex() + 1) % ui->stackedWidget->count();
    ui->stackedWidget->setCurrentIndex(nextPageIndex);
}

